package ssf.app.action.sys;

import java.util.Map;

import javax.servlet.http.*;

import org.apache.struts.action.*;

import ssf.app.action.BaseAction;
import ssf.app.form.sys.LoginForm;
import ssf.core.Constants;
import ssf.core.util.CommonUtil;

public class LoginAction extends BaseAction {
	private static final String NOT_EXISTS = "该用户不存在";// 用户不存在
	private static final String WRONG_PASS = "密码错误";// 密码错误

	private String indexUrl;

	@SuppressWarnings("unchecked")
	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {
		LoginForm loginForm = (LoginForm) form;
		String md5Pwd = CommonUtil.getMD5ofStr(loginForm.getPassword());
		Map dbUser = getServMgr().getUserService().getUserByUserName(
				loginForm.getUserName());
		if (dbUser != null && md5Pwd.equals((String) dbUser.get("password"))) {
			request.getSession().invalidate();
			request.getSession().setAttribute(Constants.LOGIN_USER, dbUser);
			if (indexUrl == null) {
				return mapping.findForward("homepage");
			}
			return mapping.findForward(Constants.RETURN_SUCCESS);
		}
		if (dbUser == null) {
			request.setAttribute("errorInfo", NOT_EXISTS);
			return mapping.findForward(Constants.RETURN_ERROR);
		}
		if (!md5Pwd.equals((String) dbUser.get("PASSWORD"))) {
			request.setAttribute("errorInfo", WRONG_PASS);
			return mapping.findForward(Constants.RETURN_ERROR);
		}
		return mapping.findForward(Constants.RETURN_ERROR);
	}

}
